import * as constants from './constants';
// import { fromJS } from 'immutable';
import {
    reqAppList,
    reqDevicesList,
    reqAPPDevicesImgList,
    reqAPPExecTimeList,
    reqNextDayExecTime,
    reqRYMStart,
    reqRYMStop,
    reqRYMSingleReStart,
    reqRYMSingleStop,
    reqAPPServiceHost,
    reqRegWifiDevicesPost,
    reqRegWifiDevicesGet,
    reqReStartADB
} from '../../../api'

const updateAppList = (data) => ({
    type: constants.REQ_APP_LIST,
    appList: data
});

const updateDevicesList = (data) => ({
    type: constants.REQ_DEVICES_LIST,
    devicesList: data
});

const updateAllImgList = (data) => ({
    type: constants.REQ_ALL_IMG_LIST,
    allImgList: data
});

const updateAPPDeviceImgList = (data) => ({
    type: constants.REQ_APP_DEVICE_IMG_LIST,
    appDeviceImgList: data
});

const updateAPPExecTimeList = (data) => ({
    type: constants.REQ_APP_EXEC_TIME_LIST,
    appExecTimeList: data
});

const updateNextDayExecTime = (data) => ({
    type: constants.REQ_NEXT_DAY_EXEC_TIME,
    nextDayExecTime: data
});

const updateSelectAppImgItem = (data) => ({
    type: constants.REQ_SELECT_APP_IMG,
    selectAppImgItem: data
});

const updateCurrentAppInfo = (data) => ({
    type: constants.REQ_CURRENT_APP_INFO,
    currentAppInfo: data
});

const updateExecHealthInfo = (data) => ({
    type: constants.REQ_EXEC_HEALTH_INFO,
    execHealthInfo: data
});

const updateAPPServiceHost = (data) => ({
    type: constants.REQ_APP_SERVICE_HOST,
    serviceHost: data
});

export const loadAppList = () => {
    return async (dispatch) => {
        const result = await reqAppList();
        if (result.code === 0) {
            dispatch(updateAppList(result.data));
        }
    }
};

export const loadDevicesList = () => {
    return async (dispatch) => {
        const result = await reqDevicesList();
        if (result.code === 0) {
            dispatch(updateDevicesList(result.data));
        }
    }
};

export const loadAPPDevicesImgList = (json) => {
    return async (dispatch) => {
        const result = await reqAPPDevicesImgList(json);
        if (result.code === 0) {
            dispatch(updateAPPDeviceImgList(result.data));
        }
    }
};

export const updateSelectAppImg = (script) => {
    return async (dispatch) => {
        dispatch(updateSelectAppImgItem(script));
    }
};

export const loadAPPExecTimeList = () => {
    return async (dispatch) => {
        const result = await reqAPPExecTimeList();
        if (result.code === 0) {
            dispatch(updateAPPExecTimeList(result.data));
        }
    }
};

export const loadNextDayExecTime = () => {
    return async (dispatch) => {
        const result = await reqNextDayExecTime();
        if (result.code === 0) {
            dispatch(updateNextDayExecTime(result.data));
        }
    }
};

export const loadRYMStart = (func) => {
    return async (dispatch) => {
        const result = await reqRYMStart();
        if (result.code === 0) {
            if (func != null) {
                func(result.data.msg)
            }
        }
    }
};

export const loadRYMStop = (func) => {
    return async (dispatch) => {
        const result = await reqRYMStop();
        if (result.code === 0) {
            if (func != null) {
                func(result.data.msg)
            }
        }
    }
};

export const loadRYMSingleReStart = (json, func) => {
    return async (dispatch) => {
        const result = await reqRYMSingleReStart(json);
        if (result.code === 0) {
            if (func != null) {
                func(result.data.msg)
            }
        }
    }
};

export const loadRYMSingleStop = (json, func) => {
    return async (dispatch) => {
        const result = await reqRYMSingleStop(json);
        if (result.code === 0) {
            if (func != null) {
                func(result.data.msg)
            }
        }
    }
};

export const loadAPPServiceHost = () => {
    return async (dispatch) => {
        const result = await reqAPPServiceHost();
        if (result.code === 0) {
            dispatch(updateAPPServiceHost(result.data));
        }
    }
};

export const loadRegWifiDevicesPost = (func) => {
    return async (dispatch) => {
        const result = await reqRegWifiDevicesPost();
        if (result.code === 0) {
            if (func != null) {
                func(result.data)
            }
        }
    }
};

export const loadRegWifiDevicesGet = (json, func) => {
    return async (dispatch) => {
        const result = await reqRegWifiDevicesGet(json);
        if (result.code === 0) {
            if (func != null) {
                func(result.data)
            }
        }
    }
};

export const loadReStartADB = (func) => {
    return async (dispatch) => {
        const result = await reqReStartADB();
        if (result.code === 0) {
            if (func != null) {
                func(result.data)
            }
        }
    }
};

//=============================================================
class Singleton {
    constructor() {
        this.instance = null;
    }

    static getInstance(dispatch) {
        if (!this.instance) {
            this.instance = new Singleton();
            Singleton.prototype.listens = [];
            let ws = new WebSocket("ws://0.0.0.0:9990/api/echo_ws");
            ws.onopen = function () {
                // console.log("发送数据");
            };

            ws.onmessage = function (evt) {
                let received_msg = evt.data;
                // console.log(received_msg);
                dispatch(updateAllImgList(received_msg));
                Singleton.prototype.listens.forEach((item, b, c) => {
                    item(received_msg);
                });
            };

            ws.onclose = function () {
                // console.log("连接已关闭...");
            };
        }
        return this.instance;
    }

    addListen(func) {
        if (func != null)
            Singleton.prototype.listens.push(func);
    }
}

export const connectionWS = (dispatch, func) => {
    Singleton.getInstance(dispatch).addListen(func);
};

//=============================================================
class SingletonExecCurr {
    constructor() {
        this.instance = null;
    }

    static getInstance(dispatch) {
        if (!this.instance) {
            this.instance = new SingletonExecCurr();
            SingletonExecCurr.prototype.listens = [];
            let ws = new WebSocket("ws://0.0.0.0:9990/api/curr_exec_ws");
            ws.onopen = function () {
                // console.log("发送数据");
            };

            ws.onmessage = function (evt) {
                let received_msg = evt.data;
                // console.log(received_msg);
                let json = JSON.parse(received_msg);
                dispatch(updateCurrentAppInfo(json));
                SingletonExecCurr.prototype.listens.forEach((item, b, c) => {
                    item(json);
                });
            };

            ws.onclose = function () {
                // console.log("连接已关闭...");
            };
        }
        return this.instance;
    }

    addListen(func) {
        if (func != null)
            SingletonExecCurr.prototype.listens.push(func);
    }
}

export const connectionExecCurrWS = (dispatch, func) => {
    SingletonExecCurr.getInstance(dispatch).addListen(func);
};

//=============================================================
class SingletonExecHealth {
    constructor() {
        this.instance = null;
    }

    static getInstance(dispatch) {
        if (!this.instance) {
            this.instance = new SingletonExecHealth();
            SingletonExecHealth.prototype.listens = [];
            let ws = new WebSocket("ws://0.0.0.0:9990/api/health_exec_ws");
            ws.onopen = function () {
                // console.log("发送数据");
            };

            ws.onmessage = function (evt) {
                let received_msg = evt.data;
                // console.log(received_msg);
                let json = JSON.parse(received_msg);
                dispatch(updateExecHealthInfo(json));
                SingletonExecHealth.prototype.listens.forEach((item, b, c) => {
                    item(json);
                });
            };

            ws.onclose = function () {
                // console.log("连接已关闭...");
            };
        }
        return this.instance;
    }

    addListen(func) {
        if (func != null)
            SingletonExecHealth.prototype.listens.push(func);
    }
}

export const connectionExecHealthWS = (dispatch, func) => {
    SingletonExecHealth.getInstance(dispatch).addListen(func);
};

//=============================================================
class SingletonRunLog {
    constructor() {
        this.instance = null;
    }

    static getInstance(dispatch) {
        if (!this.instance) {
            this.instance = new SingletonRunLog();
            SingletonRunLog.prototype.listens = [];
            let ws = new WebSocket("ws://0.0.0.0:9990/api/run_log_ws");
            ws.onopen = function () {
                // console.log("发送数据");
            };

            ws.onmessage = function (evt) {
                let received_msg = evt.data;
                // console.log(received_msg);
                // let json = JSON.parse(received_msg);
                // dispatch(updateExecHealthInfo(json));
                SingletonRunLog.prototype.listens.forEach((item, b, c) => {
                    item(received_msg);
                });
            };

            ws.onclose = function () {
                // console.log("连接已关闭...");
            };
        }
        return this.instance;
    }

    addListen(func) {
        if (func != null)
            SingletonRunLog.prototype.listens.push(func);
    }
}

export const connectionRunLogWS = (dispatch, func) => {
    SingletonRunLog.getInstance(dispatch).addListen(func);
};
